package com.beamfield.controller.manage.portal;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.NewsSection;
import com.beamfield.service.manage.portal.NewsSectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 后台门户新闻栏目管理
 */
@RequestMapping("/cms/portal/newsSection")
@Controller
public class NewsSectionController extends BaseController {

    private final NewsSectionService newsSectionService;

    @Autowired
    public NewsSectionController(NewsSectionService newsSectionService) {
        this.newsSectionService = newsSectionService;
    }

    @RequestMapping(value = "/newsSection",method = RequestMethod.GET)
    public ModelAndView newsSectionPage(){
        return new ModelAndView();
    }

    /**
     * 获取新闻栏目信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readNewsSectionData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readNewsSectionData(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        List<NewsSection> newsSectionList = newsSectionService.getNewsSectionData(offset, limit, beamId);
        long total = newsSectionService.countNewsSectionData(beamId);
        return new PageResult(total, CommonUtils.parseLong(offset,0L), newsSectionList);
    }

    /**
     * 保存新闻栏目
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request,NewsSection newsSection){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String name = newsSection.getName();
        if(StringUtils.isEmpty(name)){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "请输入栏目名称");
        }
        List<NewsSection> newsSectionByName = newsSectionService.getNewsSectionByName(name, beamId);
        if(newsSectionByName != null && !newsSectionByName.isEmpty()){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "请输入名称不同栏目");
        }
        newsSection.setBeam_id(beamId);
        newsSection.setOperator(userId);
        newsSection.setDel_flag(0);
        newsSection.setType(3);
        newsSection.setAddtime(new Date());
        int result = 0;
        try{
            result = newsSectionService.save(newsSection);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"新增成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 删除一条新闻栏目
     * @param request
     * @return
     */
    @RequestMapping(value = "/doDelete",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doDelele(HttpServletRequest request){
        Long userId = super.getUserId(request);
        String newsSectionId = request.getParameter("news_section_id");
        if(StringUtils.isEmpty(newsSectionId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        int result = 0;
        try{
            result = newsSectionService.deleteOne(newsSectionId, userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"删除成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 更新新闻栏目
     * @param request
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult update(HttpServletRequest request,NewsSection newsSection){
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        Long newsSectionId = newsSection.getNews_section_id();
        if(newsSectionId == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        String name = newsSection.getName();
        if(StringUtils.isEmpty(name)){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "请输入栏目名称");
        }
        List<NewsSection> newsSectionByName = newsSectionService.getNewsSectionByName(name, beamId);
        if(newsSectionByName != null && !newsSectionByName.isEmpty()){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), "请输入名称不同栏目");
        }
        newsSection.setUpdater(userId);
        newsSection.setUpdatetime(new Date());
        int result = 0;
        try{
            result = newsSectionService.updateOne(newsSection);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"更新成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }
}

